package com.ophiux.FeverManagement.aplication.serviceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.FeverManagement.aplication.bean.getPatientTreatmentListByDoctorRequest;
import com.ophiux.FeverManagement.aplication.bean.getPatientTreatmentListByDoctorResponse;
import com.ophiux.FeverManagement.aplication.service.IGetPatientTreatmentListByDoctorAppPort;
import com.ophiux.FeverManagement.domain.entity.DoctorInfo;
import com.ophiux.FeverManagement.domain.entity.RbPatientTreatment;
import com.ophiux.FeverManagement.domain.entity.RbPatientTreatmentVisit;
import com.ophiux.FeverManagement.domain.repository.IDoctorRepositories;
import com.ophiux.FeverManagement.domain.repository.IPatientTreatmentDetailRepository;
import com.ophiux.FeverManagement.domain.repository.IPatientTreatmentRepository;
import com.ophiux.micro.core.application.Result;
import com.ophiux.micro.core.application.ResultEnum;
import com.ophiux.utils.parser.AssemblyFactory;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class GetPatientTreatmentListByDoctorAppService implements IGetPatientTreatmentListByDoctorAppPort {
	@Autowired
	IPatientTreatmentRepository patientTreatmentRepository;
	@Autowired
	IPatientTreatmentDetailRepository patientTreatmentDetailRepository;
	@Autowired
	IDoctorRepositories doctorRepositories;
	
	@Override
	public Result<List<getPatientTreatmentListByDoctorResponse>> execute(getPatientTreatmentListByDoctorRequest request) {
		try {
			if (!request.isParamValid()) {
				return Result.fail(ResultEnum.requestParamFail);
			}
			
			DoctorInfo doctor = doctorRepositories.getDoctorByOtherId(request.getOTHER_ID());
			if(doctor==null) {
				return Result.fail(ResultEnum.NotFoundDoctorInfo);
			}
			
			Map<String,String> map = new HashMap<String,String>();
			map.put("DOCTOR_ID", doctor.getUSER_ID());
			map.put("PATIENT_NAME", request.getPATIENT_NAME());
			map.put("START_DATE", request.getSTART_DATE());
			map.put("END_DATE", request.getEND_DATE());
			
			//暂未连表操作，后续修改。。。。。
			List<RbPatientTreatmentVisit> list = patientTreatmentRepository.getAllDetaulVisit(map);
			
			List<getPatientTreatmentListByDoctorResponse> resp = AssemblyFactory.instance().parse(list, getPatientTreatmentListByDoctorResponse.class);

			return Result.success(resp);
		} catch (Exception e) {
			log.info("GetPatientTreatmentListByDoctorAppService=》execute(),程序异常：", e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException);
		}
	}

}
